﻿#include "mypushbutton_p.h"

MyPushButtonPrivate::MyPushButtonPrivate()
    : imageLayerIndex(1),
      deviceIndex(1),
      objectType(My::M),
      data(0),
      displayEnable(true),
      displayDefault(true),
      pushButtonOperate(My::Set_ON),
      coilOfRegisterIndex(0),
      textBuffer(),
      LeftmousePressed(false),
      mousePressPoint(0, 0)
{

}

MyPushButtonPrivate::~MyPushButtonPrivate()
{
}

MyPushButton::MyPushButton(QWidget *parent)
    : QPushButton(parent),
      d(new MyPushButtonPrivate)
{

}

MyPushButton::~MyPushButton()
{
    delete d;
}

/*!
    \property MyPushButton::imageLayerIndex
    \brief the index of the imageLayer

    The imageLayer is the layer of the widget.

    The default imageLayerIndex is 1.

    \sa indent
*/
int MyPushButton::getImageLayerIndex() const
{
    return d->imageLayerIndex;
}

void MyPushButton::setImageLayerIndex(int imageLayerInd)
{
    if(d->imageLayerIndex == imageLayerInd)
        return;
    d->imageLayerIndex = imageLayerInd;
}

/*!
    \property MyPushButton::deviceIndex
    \brief the index of the paired device

    The Device is the paired PLC.

    The default imageLayerIndex is 0.

    \sa indent
*/
int MyPushButton::getDeviceIndex() const
{
    return d->deviceIndex;
}

void MyPushButton::setDeviceIndex(int deviceInd)
{
    if(d->deviceIndex == deviceInd)
        return;
    d->deviceIndex = deviceInd;
}

/*!
    \property MyPushButton::objectType
    \brief the type of the object

    The object is coil or register of PLC.

    The default objectType is M.

    \sa indent
*/
My::ObjectType MyPushButton::getObjectType() const
{
    return d->objectType;
}

void MyPushButton::setObjectType(My::ObjectType type)
{
    if(d->objectType == type)
        return;
    d->objectType = type;
}

/*!
    \property MyPushButton::objectIndex
    \brief the index of the object

    The default index is 0.

    \sa indent
*/
int MyPushButton::getObjectIndex() const
{
    return d->objectIndex;
}

void MyPushButton::setObjectIndex(int index)
{
    if(d->objectIndex == index)
        return;
    d->objectIndex = index;
}

/*!
    \property MyPushButton::modbusData
    \brief the data of the Modbus Communication

    The default data is 0.

    \sa indent
*/
double MyPushButton::getData() const
{
    return d->data;
}

void MyPushButton::setData(double data)
{
    if(d->data == data)
        return;
    d->data = data;
}

void MyPushButton::setData(int data)
{
    setData((double)data);
}

/*!
    \property MyPushButton::displayEnable
    \brief control the MyLabel whether or not display

    True or False corresponds to display or undisplay.

    The default DisplayEnable is true.

    \sa indent
*/
bool MyPushButton::getDisplayEnable() const
{
    return d->displayEnable;
}

void MyPushButton::setDisplayEnable(bool displayEnabl)
{
    if(d->displayEnable == displayEnabl)
        return;
    d->displayEnable = displayEnabl;

    if(!d->displayEnable)
    {
        d->textBuffer = text();
        setText(NULL);
    }
    else
    {
        setText(d->textBuffer);
    }

    this->update();
}

/*!
    \property MyPushButton::displayDefault
    \brief control the content to display

    True or False corresponds to default or notdefault.

    The default displayDefault is true.

    \sa indent
*/
bool MyPushButton::getDisplayDefault() const
{
    return d->displayDefault;
}

void MyPushButton::setDisplayDefault(bool displayDefau)
{
    if(d->displayDefault == displayDefau)
        return;
    d->displayDefault = displayDefau;
    this->update();
}

/*!
    \property MyPushButton::getPushButtonOperate
    \brief the Operation of the PushButton

    The default getPushButtonOperate is Set_ON.

    \sa indent
*/
My::PushButtonOperate MyPushButton::getPushButtonOperate() const
{
    return d->pushButtonOperate;
}

void MyPushButton::setPushButtonOperate(My::PushButtonOperate operate)
{
    if(d->pushButtonOperate == operate)
        return;

    d->pushButtonOperate = operate;
    this->update();
}

/*!
    \property MyPushButton::getCoilOfRegisterIndex
    \brief the Index of the coil in register

    The default coilOfRegisterIndex is 0.

    \sa indent
*/
int MyPushButton::getCoilOfRegisterIndex() const
{
    return d->coilOfRegisterIndex;
}

void MyPushButton::setCoilOfRegisterIndex(int index)
{
    if(d->coilOfRegisterIndex == index)
        return;

    d->coilOfRegisterIndex = index;
}

/*!
    \reimp
*/
void MyPushButton::paintEvent(QPaintEvent *ev)
{
    //绘制准备工作,启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    if(d->displayDefault)
    {
        switch (d->pushButtonOperate)
        {
        case My::Set_ON:
            this->setText("ON");
            break;
        case My::Set_OFF:
            this->setText("OFF");
            break;
        case My::Set_REVERSE:
            this->setText("REVERSE");
            break;
        case My::Set_Instantaneous_On:
            this->setText("Instantaneous On");
            break;
        case My::Set_Data:
            this->setText("Set Data");
            break;
        case My::Interface_Jump:
            this->setText("Jump Interface");
            break;
        case My::Windows_Open:
            this->setText("OpenWindow");
            break;
        case My::Windows_Close:
            this->setText("CloseWindow");
            break;
        default:
            return;
        }
    }

    QPushButton::paintEvent(ev);
}

/*!
 * \brief MyPushButton::enterEvent
 * \param ev
 */
void MyPushButton::enterEvent(QEvent *ev)
{
    if(QEvent::Enter == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }
}

/*!
 * \brief MyPushButton::leaveEvent
 * \param ev
 */
void MyPushButton::leaveEvent(QEvent *ev)
{
    if(QEvent::Leave == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::ArrowCursor);         //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }
}

/*!
 * \brief MyPushButton::mousePressEvent
 * \param ev
 */
void MyPushButton::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = true;
        d->mousePressPoint = ev->globalPos();

        QCursor cursor;                         //创建光标对象
        cursor.setShape(Qt::ClosedHandCursor);  //设置光标形状(小手)
        setCursor(cursor);                      //使用光标

        update();
    }
}

/*!
 * \brief MyPushButton::mouseReleaseEvent
 * \param ev
 */
void MyPushButton::mouseReleaseEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = false;

        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标

        update();
    }

}

/*!
 * \brief MyPushButton::mouseMoveEvent
 * \param ev
 */
void MyPushButton::mouseMoveEvent(QMouseEvent *ev)
{
    // 鼠标左键拖动移动
    if(d->LeftmousePressed)
    {
        double mouseDx = ev->globalPos().x() - d->mousePressPoint.x();
        double mouseDy = ev->globalPos().y() - d->mousePressPoint.y();
        d->mousePressPoint = ev->globalPos();

        double myLabelNewx = this->x() + mouseDx;
        double myLabelNewy = this->y() + mouseDy;

        if(myLabelNewx < 0)
            myLabelNewx = 0;
        if(myLabelNewx + this->width() > QApplication::activeWindow()->width())
            myLabelNewx = QApplication::activeWindow()->width() - this->width();
        if(myLabelNewy < 0)
            myLabelNewy = 0;
        if(myLabelNewy + this->height() > QApplication::activeWindow()->height())
            myLabelNewy = QApplication::activeWindow()->height() - this->height();

        move(myLabelNewx, myLabelNewy);
        update();
    }
}
